﻿
using System.Collections.Generic;
using System.Linq;
using studentSubsidy.Models;
using studentSubsidy.Utils;

namespace studentSubsidy.Dao
{
    public class AdminFinancialDao
    {
        private MyDbContext DbContext = new MyDbContext();

        public List<Apply> getCheckedApply(string role)
        {
            if (role == "school")
            {
                return DbContext.Applies.Where(p => p.isCheck == 1 && p.isCityCheck == -1 && p.isPayment==0).ToList();
            }
            if (role == "bureau")
            {
                return DbContext.Applies.Where(p => p.isCheck == 1 && p.isCityCheck == 1 && p.isPayment==0).ToList();
            }
            return null;
        }

        public void putPayment(Payment payment)
        {
            DbContext.Payment.Add(payment);
            Apply apply = DbContext.Applies.Where(p => p.apply_id == payment.apply_id).ToList().First();
            apply.isPayment = 1;
            DbContext.Applies.Update(apply);
            DbContext.SaveChanges();
        }
        
        public List<Apply> getHasPaymentApply(string role)
        {
            if (role == "school")
            {
                return DbContext.Applies.Where(p => p.isCheck == 1 && p.isCityCheck == -1 && p.isPayment==1).ToList();
            }
            if (role == "bureau")
            {
                return DbContext.Applies.Where(p => p.isCheck == 1 && p.isCityCheck == 1 && p.isPayment==1).ToList();
            }
            return null;
        }

        public List<Apply> getRefundRoleList(string role)
        {
            if (role == "school")
            {
                return DbContext.Applies.Where(p =>  p.isCityCheck == -1 && p.isPayment==1 && p.isSigned == 1).ToList();
            }
            if (role == "bureau")
            {
                return DbContext.Applies.Where(p =>  p.isCityCheck == 1 && p.isPayment==1 && p.isSigned == 1).ToList();
            }
            return null;
        }
        
        public Refund getRefundList(int applyId)
        {
            return DbContext.Refund.Where(p => p.apply_id == applyId).ToList().First();
        }

        public void putRefund(Refund refund)
        {
            DbContext.Refund.Add(refund);
            DbContext.SaveChanges();
        }

        public void putRefundLog(Refund_Log refundLog)
        {
            DbContext.RefundLogs.Add(refundLog);
            DbContext.SaveChanges();
        }

        public Refund getRefundByApplyId(int applyId)
        {
            return DbContext.Refund.Where(p => p.apply_id == applyId).ToList().First();
        }

        public List<Refund_Log> getRefundLogByRefundId(int refund_id)
        {
            return DbContext.RefundLogs.Where(p => p.refund_id == refund_id && p.hasRefund !=1).ToList();
        }

        public void updateRefundState(Refund refund)
        {
            DbContext.Update(refund);
            DbContext.SaveChanges();
        }

        public void updateRefundLog(Refund_Log refundLog)
        {
            DbContext.Update(refundLog);
            DbContext.SaveChanges();
        }
        
    }
}